package blog.service.impl;

import blog.mapper.CollectMapper;
import blog.pojo.Collects;
import blog.pojo.ResultVo;
import blog.service.CollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author HuLin
 * @date
 */
@Service
public class CollectServiceImpl implements CollectService {
	@Autowired
	private CollectMapper collectMapper;

    @Override
    public ResultVo addCollect(Collects coll) {
//        uid,bid 查收藏，有则-，无则+
	  QueryWrapper queryWrapper = new QueryWrapper();
	  queryWrapper.eq("uid", coll.getUid());
	  queryWrapper.eq("bid", coll.getBid());
	  Collects collects1 = collectMapper.selectOne(queryWrapper);
	  if(collects1 != null){//有
		int i = collectMapper.deleteById(coll.getId());
		if(i>0){
		    return new ResultVo(0,"concel success");
		}else{
		return new ResultVo(100,"concel success");}
	  }else {//无
		coll.setCreateTime(new Date());
		int insert = collectMapper.insert(coll);
		if (insert>0){

		    return new ResultVo(0,"add success");
		}
		return new ResultVo(100,"add fail");
	  }
    }
}
